package com.bdqn.tour.servlet.Hzy;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.tour.pojo.HzyHotel;
import com.bdqn.tour.pojo.HzyScenicSpot;
import com.bdqn.tour.pojo.HzyTickets;
import com.bdqn.tour.service.impl.HzyOrdersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/updateOrder")
public class OrderUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置编码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        // 创建JSON对象用于封装返回数据
        JSONObject result = new JSONObject();

        // 获取请求参数
        String orderId = req.getParameter("orderId");
        String orderType = req.getParameter("orderType");

        // 移除可能的前缀字母
        orderId = orderId.replaceAll("^[HST]", "");

        boolean success = false;

        // 根据订单类型更新订单数据
        HzyOrdersServiceImpl ordersService = new HzyOrdersServiceImpl();

        switch (orderType) {
            // 更新酒店订单数据
            case "hotel":
                HzyHotel hotel = new HzyHotel();
                hotel.setId(Integer.parseInt(orderId));
                hotel.setRoomNum(Integer.parseInt(req.getParameter("roomNum")));
                hotel.setContactPhone(req.getParameter("contactPhone"));
                hotel.setContactName(req.getParameter("contactName"));
                hotel.setIdCard(req.getParameter("idCard"));
                hotel.setPrice(Double.parseDouble(req.getParameter("price")));
                hotel.setBrand(req.getParameter("brand"));
                success = ordersService.updateHotelOrder(hotel);
                break;
            // 更新景点订单数据
            case "scenic":
                HzyScenicSpot scenic = new HzyScenicSpot();
                scenic.setId(Integer.parseInt(orderId));
                scenic.setScenicName(req.getParameter("scenicName"));
                scenic.setScenicAddress(req.getParameter("scenicAddress"));
                scenic.setPrice(Double.parseDouble(req.getParameter("price")));
                scenic.setCity(req.getParameter("city"));
                success = ordersService.updateScenicOrder(scenic);
                break;

            // 更新门票订单数据
            case "ticket":
                HzyTickets ticket = new HzyTickets();
                ticket.setId(Integer.parseInt(orderId));
                ticket.setType(req.getParameter("type"));
                ticket.setTicketType(req.getParameter("ticketType"));
                ticket.setDeparture_city(req.getParameter("departureCity"));
                ticket.setArrival_city(req.getParameter("arrivalCity"));
                ticket.setDeparture_time(req.getParameter("departureTime"));
                ticket.setPrice(Double.parseDouble(req.getParameter("price")));
                success = ordersService.updateTicketOrder(ticket);
                break;
        }
        // 封装返回结果
        result.put("success", success);
        result.put("message", success ? "更新成功" : "更新失败");

        // 将结果返回给客户端
        resp.getWriter().write(result.toJSONString());
    }
}
